package com.broad.commerce.code.services.base;

import cn.hutool.core.collection.CollUtil;
import com.broad.commerce.code.base.EntityModuleCreator;
import com.broad.commerce.code.models.EntityModuleDTO;
import com.broad.commerce.code.modules.JdbcModule;
import com.broad.commerce.code.modules.jdbc.EntityModule;
import com.broad.commerce.code.services.ProjectRootDirService;
import com.broad.commerce.code.services.jdbc.EntityModelService;
import com.broad.commerce.code.services.jdbc.EntityModuleService;
import com.broad.commerce.code.services.jdbc.FastMybatisModuleService;
import com.broad.commerce.code.services.jdbc.JpaModuleService;
import com.broad.commerce.code.services.jdbc.MapperModuleService;
import com.broad.commerce.code.services.jdbc.MybatisModuleService;
import com.broad.commerce.common.utils.core.StringUtils;
import com.broad.commerce.web.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-06 23:01
 * @description: CodeService
 **/
@Service
public class JdbcModuleService {
	@Autowired
	private EntityModuleService entityModuleService;
	@Autowired
	private EntityModelService entityModelService;
	@Autowired
	private JpaModuleService jpaModuleService;
	@Autowired
	private MybatisModuleService mybatisModuleService;
	@Autowired
	private MapperModuleService mapperModuleService;
	@Autowired
	private FastMybatisModuleService fastMybatisModuleService;
	@Autowired
	private DataBaseTableService dataBaseTableService;

	public void createModuleFiles(EntityModuleDTO entityModuleDTO) {
		String rootPath = ProjectRootDirService.PROJECT_DIR_MAP.get(entityModuleDTO.getJdbcProject());
		String filePath = rootPath + "/src/main/java/";
		String xmlPath = rootPath + "/src/main/resources/mybatis/";
		if (StringUtils.isNotEmpty(entityModuleDTO.getXmlParent())) {
			xmlPath += entityModuleDTO.getXmlParent() + "/";
		}
		JdbcModule jdbcModule = this.initJdbcModule(entityModuleDTO);
		this.entityModuleService.createClassFile(jdbcModule.getEntityModule(), filePath, "java");
		this.entityModelService.createClassFile(jdbcModule.getEntityModel(), filePath, "java");
		this.jpaModuleService.createClassFile(jdbcModule.getJpaModule(), filePath, "java");
		this.mybatisModuleService.createClassFile(jdbcModule.getMybatisModule(), filePath, "java");
		if (StringUtils.isNotEmpty(jdbcModule.getFastMybatisPackage())) {
			this.fastMybatisModuleService.createClassFile(jdbcModule.getFastMybatisModule(), filePath, "java");
		}
		this.mapperModuleService.createClassFile(jdbcModule.getMapperModule(), xmlPath, "xml");
		this.createTable(jdbcModule.getEntityModule());
	}

	private Boolean checkTableExists(String tableName) {
		List<String> tableNames = dataBaseTableService.searchAllTableNames();
		return CollUtil.isNotEmpty(tableNames) && tableNames.contains(tableName.toLowerCase());
	}

	private void createTable(EntityModule entityModule) {
		boolean isExists = this.checkTableExists(entityModule.getTableName());
		if (isExists && entityModule.getUpdate()) {
			dataBaseTableService.dropTable(entityModule.getTableName());
			isExists = false;
		}
		if (!isExists) {
			String createSql = dataBaseTableService.createTableDDL(entityModule);
			dataBaseTableService.createTable(createSql);
		}
	}

	public JdbcModule initJdbcModule(EntityModuleDTO entityModuleDTO) {
		EntityModule entityModule = EntityModuleCreator.createEntityModule(entityModuleDTO);
		String entityPackage = entityModuleDTO.getPackageName();
		if (!entityPackage.contains("entity")) {
			throw new ServiceException("实体类包名中必须含有 entity 字段！");
		}
		String modelPackage = entityPackage.replace("entity", "model");
		String jpaPackage = entityPackage.replace("entity", "jpa");
		String mybatisPackage = entityPackage.replace("entity", "mybatis");
		String fastMybatisPackage = entityPackage.replace("entity", "fastmybatis");
		//@off
		JdbcModule jdbcModule = JdbcModule.builder()
		                                  .entityModule(entityModule)
		                                  .modelPackage(modelPackage)
		                                  .jpaPackage(jpaPackage)
			                              .mybatisPackage(mybatisPackage)
			                              .fastMybatisPackage(fastMybatisPackage)
		                                  .build();//@on
		jdbcModule.initJdbcModule();
		return jdbcModule;
	}
}
